package com.portal.incurridos.persistence.dao.impl;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Component;

import com.portal.incurridos.persistence.dao.WallDao;
import com.portal.incurridos.persistence.enumeration.StateTransactionEnum;
import com.portal.incurridos.persistence.wall.Wall;

/**
 * @author Alan Dávila
 * e-mail ustargab@gmail.com
 * date 21/11/2013 18:25:22
 */
@Component("wallDaoImpl")
public class WallDaoHibernateImpl implements WallDao {

	/**
	 * Managment bean of dao layer.
	 */
	@Resource(name = "sessionFactory")
    private SessionFactory factoria;

	/* (non-Javadoc)
	 * @see com.portal.incurridos.persistence.dao.WallDao#get(java.lang.Integer)
	 */
	public final Wall get(final Integer wall) {
		Session sesion = null;
		Wall wallOut = null;
		try {
			sesion = factoria.getCurrentSession();
			wallOut = (Wall) sesion.get(Wall.class, wall);
			sesion.close();
		} catch (Exception e) {
			wallOut = null;
		}
		return wallOut;
	}

	/* (non-Javadoc)
	 * @see com.portal.incurridos.persistence.dao.WallDao#create(com.portal.incurridos.persistence.wall.Wall)
	 */
	public final Wall create(final Wall wall) {
		Session sesion = null;
		Transaction tx = null;
		Wall wallOut = wall;
		try {
			sesion = factoria.getCurrentSession();
			tx = sesion.beginTransaction();
			sesion.save(wallOut);
			tx.commit();
			sesion.close();
		} catch (Exception e) {
			wallOut = null;
		}
		return wallOut;
	}

	/* (non-Javadoc)
	 * @see com.portal.incurridos.persistence.dao.WallDao#update(com.portal.incurridos.persistence.wall.Wall)
	 */
	public final StateTransactionEnum update(final Wall wall) {
		Session sesion = null;
		Transaction tx = null;
		try {
			sesion = factoria.getCurrentSession();
			tx = sesion.beginTransaction();
			sesion.update(wall);
			tx.commit();
			sesion.close();
		} catch (Exception e) {
			return StateTransactionEnum.ERROR;
		}
		return StateTransactionEnum.UPDATED;
	}

	/* (non-Javadoc)
	 * @see com.portal.incurridos.persistence.dao.WallDao#delete(com.portal.incurridos.persistence.wall.Wall)
	 */
	public final StateTransactionEnum delete(final Wall wall) {
		Session sesion = null;
		Transaction tx = null;
		try {
			sesion = factoria.getCurrentSession();
			tx = sesion.beginTransaction();
			sesion.delete(wall);
			tx.commit();
			sesion.close();
		} catch (Exception e) {
			return StateTransactionEnum.ERROR;
		}
		return StateTransactionEnum.DELETED;
	}

}
